package mobile.pubcrawler.ui;

import mobile.pubcrawler.PubCrawlerUI;
import mobile.pubcrawler.R;
import mobile.pubcrawler.model.DataModel;
import mobile.pubcrawler.model.DataModelLocation;
import android.app.Activity;
import android.os.Bundle;
import android.text.Html;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RatingBar;
import android.widget.TextView;

public class CurrentPub extends Activity {

	 @Override
	 public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        int position = getIntent().getIntExtra("position", 0);
        DataModelLocation location = DataModel.getInstance().getLocationList().get(position);
        
        setContentView(R.layout.currentpub);
        this.setTitle(R.string.currentPubActivityTitle);
    	
        ImageView logo = (ImageView)findViewById(R.id.currentPub_logo); 
        logo.setImageResource(PubCrawlerUI.getInstance().getImageResource(location.getCategory()));
        
        TextView description = (TextView)findViewById(R.id.currentPub_description); 
        description.setText(Html.fromHtml(location.getName()));
        description.setTextSize(14);
        
        TextView address = (TextView)findViewById(R.id.currentPub_address); 
        address.setText(Html.fromHtml(location.getAddress()+"<br>"+location.getPostalCode()+" "+location.getCity()));
        address.setTextSize(12);
        
        TextView distance = (TextView)findViewById(R.id.currentPub_distance); 
        distance.setText(String.valueOf(location.getDistance())+"m");
        distance.setTextSize(12);
        
        TextView moreInfo = (TextView)findViewById(R.id.currentPub_moreInfo); 
        String info="";
        String phone1 = location.getPhone1();
        String phone2 = location.getPhone2();
        if (phone1 != null) info+=getString(R.string.phone)+": "+phone1;
        if (phone2 != null) info+=", "+phone2;
        info+="<br>"+getString(R.string.category)+": "+location.getCategory();
        moreInfo.setText(Html.fromHtml(info));
        moreInfo.setTextSize(12);
        
        TextView comments = (TextView)findViewById(R.id.currentPub_comments);
        comments.setTextSize(12);
        comments.setText(location.getRating().size()+" "+getString(R.string.reviews));
        
        RatingBar rating = (RatingBar)findViewById(R.id.currentPub_small_ratingbar); 
        rating.setRating(location.getOverallRating());
        
        LinearLayout linear1 = (LinearLayout)findViewById(R.id.currentPub_ratingandcomments);
        linear1.setOnClickListener(new detailsListener(position));
        
    	ImageButton mapButton = (ImageButton)this.findViewById(R.id.currentPub_MapButton);
    	mapButton.setOnClickListener(currPubMapButtonListener);
    	
    	ImageButton routeButton = (ImageButton)this.findViewById(R.id.currentPub_RouteButton);
    	routeButton.setOnClickListener(currPubRouteButtonListener);
    	 
    	ImageButton editSmsButton = (ImageButton)this.findViewById(R.id.currentPub_SmsButton);
    	editSmsButton.setOnClickListener(settingsEditSmsButtonListener);
    	editSmsButton.setBackgroundDrawable(null);
    	LinearLayout layout_sms = (LinearLayout)this.findViewById(R.id.currentPub_layout_sms);
    	layout_sms.setOnClickListener(settingsEditSmsButtonListener);
    	
    	ImageButton startFbButton = (ImageButton)this.findViewById(R.id.currentPub_FbButton);
    	startFbButton.setOnClickListener(PubCrawlerUI.getInstance().startFbButtonListener);
    	startFbButton.setBackgroundDrawable(null);
    	LinearLayout layout_fb = (LinearLayout)this.findViewById(R.id.currentPub_layout_fb);
    	layout_fb.setOnClickListener(PubCrawlerUI.getInstance().startFbButtonListener);
    	
	 }
	 
	    public class detailsListener implements OnClickListener {
	    	int position;
	    	public detailsListener(int pos){
	    		position = pos;
	    	}
	        public void onClick(View v) {
	        	PubCrawlerUI.getInstance().startPubDetails(position);
	        }
	    }
	 
	 private OnClickListener currPubMapButtonListener =  new OnClickListener(){
		 public void onClick(View v) { /*Code which starts google maps*/} 
	 };
	 
	 private OnClickListener currPubRouteButtonListener =  new OnClickListener(){
	     public void onClick(View v) { /*Code which starts route to pub*/}
	 };
	        
     private OnClickListener settingsEditSmsButtonListener =  new OnClickListener() {
    	 public void onClick(View v) { PubCrawlerUI.getInstance().startEditSmsUI(); } 
     };
     
     //----------------- Menu inflaters and listeners -------------------
     public boolean onCreateOptionsMenu(Menu menu) {
         MenuInflater inflater = getMenuInflater();
         inflater.inflate(R.menu.mainmenu, menu);
         return true;
     }
     
     /* Handles item selections */
     public boolean onOptionsItemSelected(MenuItem item) {
         switch (item.getItemId()) {
         case R.id.menuMainMenu:
         	PubCrawlerUI.getInstance().startMainUI();
             return true;
         case R.id.menuSettings:
         	PubCrawlerUI.getInstance().startSettingsUI();
             return true;
         case R.id.menuQuit:
             System.exit(0);
             return true;
         }
         return false;
     }
	  
}
